<%@page language="java" import="com.scalar.chiptrack.sales.SalesOrderInvoice,
                                com.scalar.chiptrack.users.User,
                                com.scalar.chiptrack.users.Functionality,
                                java.util.ArrayList" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>

<%@ page import="com.scalar.chiptrack.utils.ConfigManager"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>
<HEAD><TITLE>Scalarsoft - ChipTrac</TITLE>

<%String chipTracContext = "/" + ConfigManager.getChipTracContext();%>

<chiptrack:checkLogon sourcePage="/jsp/finance/sales_order_invoice_search.jsp"/>
<%
    request.setAttribute("YearList",SalesOrderInvoice.getYearList());
%>

<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<LINK href="<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css" type=text/css rel=stylesheet>

<SCRIPT language="JavaScript">
function submit()
{
    document.salesOrderInvoiceForm.action=" <%=chipTracContext%>/salesOrderInvoice.do?action=packingSlipInvoiceSearch";
    //document.salesOrderInvoiceForm.submit();
    return true;

}
function reset()
{
    document.salesOrderInvoiceForm.reset();
}

function generateSalesInvoice()
{
    var isPackingSlipSelected = false;

    //checking if any packing slip has been selected
    for(var i = 0 ; i >= 0 ; i++ )
    {
        var tempField = document.salesOrderInvoiceForm['salesOrderInvoice.salesOrderInvoiceLineItemList[' + i + '].customerShippingLabel'];
        if( tempField == null )
        {
            break;
        }
        else
        {
            if( document.salesOrderInvoiceForm[ 'salesOrderInvoice.salesOrderInvoiceLineItemList[' + i + '].lineItemSave' ].checked  == true )
            {
                document.salesOrderInvoiceForm[ 'salesOrderInvoice.salesOrderInvoiceLineItemList[' + i + '].lineItemSave' ].disabled  = false;
                isPackingSlipSelected = true;
                //break;
            }
        }
    }

    if( isPackingSlipSelected )
    {
        document.salesOrderInvoiceForm.action=" <%=chipTracContext%>/salesOrderInvoice.do?action=generateSalesInvoice";
        document.salesOrderInvoiceForm.submit();
    }
    else
    {
        var pa =  document.getElementById("errors");
        pa.innerHTML = " <li> Please select Sales Order and atleast one packing slip for generating sales invoice ";
    }
}

function packingSlipSelected( packingslipLabel, selectedSalesOrder )
{

    var selectedPackingSlip = packingslipLabel.value;


    //Enabling the line items pertaining to the packing slip selected.
    //if no packing slip is selected then the selectedPackingSlip value will be empty which would disable all the line item fields

    for(var i = 0 ; i >= 0 ; i++ )
    {
        var tempField = document.salesOrderInvoiceForm['salesOrderInvoice.salesOrderInvoiceLineItemList[' + i + '].customerShippingLabel'];
        if( tempField == null )
        {
            break;
        }
        else
        {
            if( document.salesOrderInvoiceForm[ 'salesOrderInvoice.salesOrderInvoiceLineItemList[' + i + '].salesOrderNumberAndGpLocation' ].value == selectedSalesOrder )
            {
                if( document.salesOrderInvoiceForm[ 'salesOrderInvoice.salesOrderInvoiceLineItemList[' + i + '].customerShippingLabel' ].value == selectedPackingSlip )
                {

                    if(packingslipLabel.checked == true)
                    {
                        //document.salesOrderInvoiceForm[ 'salesOrderInvoice.salesOrderInvoiceLineItemList[' + i + '].lineItemSave' ].disabled = true;
                        document.salesOrderInvoiceForm[ 'salesOrderInvoice.salesOrderInvoiceLineItemList[' + i + '].lineItemSave' ].checked  = true;
                    }
                    else
                    {
                        //document.salesOrderInvoiceForm[ 'salesOrderInvoice.salesOrderInvoiceLineItemList[' + i + '].lineItemSave' ].disabled = true;
                        document.salesOrderInvoiceForm[ 'salesOrderInvoice.salesOrderInvoiceLineItemList[' + i + '].lineItemSave' ].checked  = false;
                    }
                }
                /*else
                {
                    document.salesOrderInvoiceForm[ 'salesOrderInvoice.salesOrderInvoiceLineItemList[' + i + '].lineItemSave' ].disabled = true;
                    document.salesOrderInvoiceForm[ 'salesOrderInvoice.salesOrderInvoiceLineItemList[' + i + '].lineItemSave' ].checked  = false;
                }*/
            }
        }
    }


    //Deselecting all the other packing slip check boxes if the present one is selected

    /*if( packingslipLabel.checked == true )
    {
        for( var i = 0; i < document.salesOrderInvoiceForm.salesOrderSelect.length; i++ )
        {
            if( document.salesOrderInvoiceForm.salesOrderSelect[ i ].value != selectedPackingSlip )
            {
                document.salesOrderInvoiceForm.salesOrderSelect[ i ].checked = false;
            }
        }
    }*/

}


function salesOrderSelected( salesOrder )
{
    var selectedSalesOrder = "";
    if( salesOrder.checked == true )
    {
        selectedSalesOrder = salesOrder.value;
    }

    var length = document.salesOrderInvoiceForm.packingSlipSelect.length;
    //Enabling the line items pertaining to the packing slip selected.
    //if no packing slip is selected then the selectedPackingSlip value will be empty which would disable all the line item fields
    for( var j =0; j< length; j++ )
    {
        if(  document.salesOrderInvoiceForm.salesOrder[j].value == selectedSalesOrder )
        {
            document.salesOrderInvoiceForm.packingSlipSelect[j].disabled = false;
            document.salesOrderInvoiceForm.packingSlipSelect[j].checked = true;

        }
        else
        {
            document.salesOrderInvoiceForm.packingSlipSelect[j].disabled = true;
            document.salesOrderInvoiceForm.packingSlipSelect[j].checked = false;
        }

    }
    for( var i = 0 ; i >= 0 ; i++ )
    {
        var tempField = document.salesOrderInvoiceForm['salesOrderInvoice.salesOrderInvoiceLineItemList[' + i + '].customerShippingLabel'];
        if( tempField == null )
        {
            break;
        }
        else
        {
            if( document.salesOrderInvoiceForm[ 'salesOrderInvoice.salesOrderInvoiceLineItemList[' + i + '].salesOrderNumberAndGpLocation' ].value == selectedSalesOrder )
            {

                //document.salesOrderInvoiceForm[ 'salesOrderInvoice.salesOrderInvoiceLineItemList[' + i + '].lineItemSave' ].disabled = true;
                document.salesOrderInvoiceForm[ 'salesOrderInvoice.salesOrderInvoiceLineItemList[' + i + '].lineItemSave' ].checked  = true;

            }
            else
            {
                //document.salesOrderInvoiceForm[ 'salesOrderInvoice.salesOrderInvoiceLineItemList[' + i + '].lineItemSave' ].disabled = true;
                document.salesOrderInvoiceForm[ 'salesOrderInvoice.salesOrderInvoiceLineItemList[' + i + '].lineItemSave' ].checked  = false;
            }
        }
    }
}

function onFormLoad()
{
    for( var i = 0 ; i >= 0 ; i++ )
    {
        var tempField = document.salesOrderInvoiceForm['salesOrderInvoice.salesOrderInvoiceLineItemList[' + i + '].customerShippingLabel'];
        if( tempField == null )
        {
            break;
        }
        else
        {
            //if( document.salesOrderInvoiceForm[ 'salesOrderInvoice.salesOrderInvoiceLineItemList[' + i + '].lineItemSave' ].checked == true )
            {
                document.salesOrderInvoiceForm[ 'salesOrderInvoice.salesOrderInvoiceLineItemList[' + i + '].lineItemSave' ].disabled  = true;
            }
        }
    }
    if(document.salesOrderInvoiceForm.packingSlipSelect != null)
    {
        if( document.salesOrderInvoiceForm.packingSlipSelect.length == null )
        {
            document.salesOrderInvoiceForm.packingSlipSelect.disabled = true;
        }
        else
        {
            for( var i=0; i< document.salesOrderInvoiceForm.packingSlipSelect.length; i++ )
            {
                if( document.salesOrderInvoiceForm.packingSlipSelect[i].checked == true )
                {
                    document.salesOrderInvoiceForm.packingSlipSelect[i].disabled = false;
                }
                else
                {
                    document.salesOrderInvoiceForm.packingSlipSelect[i].disabled = true;
                }
            }
        }
    }

}

function getIndex( controlName )
{
    var start = controlName.lastIndexOf("["); // Get the Starting index no of [
    var end = controlName.lastIndexOf("]");   // Get the Ending index no of ]
    return parseInt (controlName.substring( start + 1, end ) );
}
</SCRIPT>

</HEAD>
<BODY bgColor=#ffffff leftMargin=0 topMargin=0 marginheight="0" marginwidth="0" onload="javascript:onFormLoad()">
<!-- Top Header Start-->

<%@include file="/jsp/includes/header.jsp"  %>

<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
  <TBODY>
    <TR>
	    <TD width='180' valign='top' background='<%=chipTracContext%>/jsp/includes/images/left_red_bg.gif'>

<!-- Side Navigation start -->

<%@include file="/jsp/sales/sales_menu.jsp" %>

        </TD>
<!-- end of the side navigations -->

        <TD bgcolor="#ffffff">
            <IMG height=9 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=8>
        </TD>
        <TD bgcolor="#ffffff">
            <IMG height=480 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=2>
        </TD>
        <TD valign="top" width="100%">

        <!-- SiteMap Tag -->
        <!-- chiptrack:sitemap styleClass="NArialS"/ -->

    <chiptrack:permission functionality='<%=Functionality.SALES_ORDER_INVOICE%>'
        action='<%=User.USER_ACTION_VIEW%>' styleClass="NArialL" >
    </chiptrack:permission>

            <IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/clear.gif" width=500 border=0>
<BR>

<!-- from here your body starts -->
<P>
    <TABLE BORDER="0" WIDTH="100%">

        <html:form name="salesOrderInvoiceForm" action="/salesOrderInvoice.do?action=packingSlipInvoiceSearch" method="post" type="com.scalar.chiptrack.sales.forms.SalesOrderInvoiceForm" onsubmit="javascript:return submit()">
        <nested:nest property="salesOrderInvoice">
        <TR>
            <TD WIDTH="100%" class="NArialW"><P ALIGN="CENTER">Shipped but not invoiced Sales Orders
            </TD>
        </TR>
        <TR>
            <TD WIDTH="100%">&nbsp;</TD>
        </TR>
        <TR>
            <TD><chiptrack:info styleClass="NArialL" /></TD>
        </TR>
        <TR>
            <TD><chiptrack:errors styleClass="NArialL" /></TD>
        </TR>
        <TR>
            <TD><chiptrack:moreErrorInfo styleClass="NArialL" /></TD>
        </TR>
        <TR>
            <TD align="left" class='NArialL'>
                <FONT id="errors" styleClass="NArialL" color='FF0000'></FONT>
            </TD>
        </TR>
        <TR>
	        <TD WIDTH="100%">
                <TABLE BORDER="0" WIDTH="100%">
				<TR>
					<TD WIDTH="100%">
						<TABLE BORDER="0" WIDTH="100%" CLASS="NArialL">
							<TR>
								<TD WIDTH="5%">&nbsp;</TD>
								<TD WIDTH="12%">Packing Slip:</TD>
								<TD WIDTH="1%">&nbsp;</TD>
								<TD WIDTH="22%">
                                    <html:text property="packingSlipLabel" styleClass="txfield" size="20" />
                                </TD>
								<TD WIDTH="1%">&nbsp;</TD>
								<TD WIDTH="14%">Sales Order #:</TD>
								<TD WIDTH="1%">&nbsp;</TD>
								<TD WIDTH="30%">
                                    <html:text property="salesOrderNumber" styleClass="txfield" size="20" />
								</TD>
								<TD WIDTH="8%">&nbsp;</TD>
							</TR>
                            <TR>
                                <TD WIDTH="5%">&nbsp;</TD>
                                <TD WIDTH="14%">Customer PO #:</td>
                                <TD WIDTH="1%">&nbsp;</TD>
                                <td WIDTH="30%"><font class="NArialL" nowrap>
                                    <html:text  property="customerPONumber" styleClass="txfield" size="20" />
                                </td>
                                <TD WIDTH="1%">&nbsp;</TD>
								<TD WIDTH="14%">Customer:</TD>
								<TD WIDTH="1%">&nbsp;</TD>
								<TD WIDTH="30%" nowrap>
                                    <html:select property="customerId" styleClass="txfield" >
                                        <html:options collection = "customerList" property="value" labelProperty="label"/>
                                    </html:select>
                                </TD>
                            </TR>
                            <TR>
								<TD WIDTH="5%">&nbsp;</TD>
								<TD WIDTH="14%">Locations:</TD>
								<TD WIDTH="1%">&nbsp;</TD>
								<TD WIDTH="30%">
                                    <html:select property="locationId" styleClass="txfield" >
                                        <html:option value="0" >--All Locations--</html:option>
                                        <html:options collection = "locations" property="value" labelProperty="label"/>
                                    </html:select>
                                </TD>
                                <TD WIDTH="1%">&nbsp;</TD>
								<TD WIDTH="12%">Year:</TD>
								<TD WIDTH="1%">&nbsp;</TD>
								<TD WIDTH="22%">
                                    <html:select property="year" styleClass="txfield" >
                                        <html:option value="" >--select--</html:option>
                                        <html:options collection = "YearList" property="value" labelProperty="label"/>
                                    </html:select>
                                </TD>

							</TR>
							<TR>
								<TD COLSPAN="9">&nbsp;</TD>
							</TR>
						</TABLE>
					</TD>
				</TR>
				<TR>
					<TD WIDTH="100%">
						<TABLE BORDER="0" WIDTH="90%" CLASS="NArialL" align="center">
							<TR>
								<TD COLSPAN="2"><P ALIGN="RIGHT">
                                <%--<A href="javascript:submit();" onmouseover="window.status='Click here to go Search for Sales Order Invoices';return true" onmouseout="window.status='';return true"><IMG SRC="<%=chipTracContext%>/jsp/includes/images/search.gif" ALIGN="BOTTOM" BORDER="0" ></a> --%>
                                <input type=image border="0" src="<%=chipTracContext%>/jsp/includes/images/search.gif" width="53" height="17" onmouseover="window.status='Click to Search for Sales Order Invoices';return true" onmouseout="window.status='';return true" title="Click here to Search for Sales Order Invoices"/>
                                </TD>
								<TD WIDTH="52%"><A href="javascript:reset();" onmouseover="window.status='Click to Reset';return true" onmouseout="window.status='';return true" title="Click here to Reset"><IMG SRC="<%=chipTracContext%>/jsp/includes/images/reset.gif" ALIGN="BOTTOM" BORDER="0"></A></TD>
							</TR>
							<TR>
								<TD COLSPAN="2">&nbsp;</TD>
								<TD WIDTH="52%">&nbsp;</TD>
							</TR>
							<TR>
								<TD coslpan="3">&nbsp;</TD>
							</TR>
						</TABLE>
					</TD>
				</TR>
                <TR>
                    <TD WIDTH="100%">
                        <TABLE BORDER="0" WIDTH="100%" CLASS="NArialL" align="center">



                            <nested:iterate property="salesOrderInvoiceLineItemList" id="invoiceLineItem" length="1"  type="com.scalar.chiptrack.sales.SalesOrderInvoiceLineItem" >
                            <TR>
                               <TD colspan="8" align="right">
                                  <A href="javascript:generateSalesInvoice()" onmouseover="window.status='Click to Generate Sales Invoice';return true" onmouseout="window.status='';return true" >Generate Sales Invoice </A>
                               </TD>
                            </TR>
                            <TR width="100%">
                                <TD BGCOLOR="#993333" colspan="2">
                                    <P ALIGN="CENTER"><FONT COLOR="white">Sales Order Number</FONT>
                                </TD>
                                <TD WIDTH="17%" BGCOLOR="#993333">
									<P ALIGN="CENTER"><FONT COLOR="white">Packing Slip</FONT>
								</TD>
                                <TD WIDTH="20%" BGCOLOR="#993333">
									<P ALIGN="CENTER"><FONT COLOR="white">Mfg. Part Number</FONT>
								</TD>
                                <TD WIDTH="25%" BGCOLOR="#993333">
									<P ALIGN="CENTER"><FONT COLOR="white">Description</FONT>
								</TD>
                                <TD WIDTH="8%" BGCOLOR="#993333">
									<P ALIGN="CENTER"><FONT COLOR="white">Quantity</FONT>
								</TD>
                                <TD WIDTH="8%" BGCOLOR="#993333" >
									<P ALIGN="CENTER"><FONT COLOR="white">Price</FONT>
								</TD>
                                <TD WIDTH="10%" BGCOLOR="#993333" >
									<P ALIGN="CENTER"><FONT COLOR="white">Extended Price</FONT>
								</TD>
                                <TD width='8%' >&nbsp;</TD>
                            </TR>
                            </nested:iterate>
                            <%
                                int currentCustomerShippingID = 0;
                                String salesOrderNumber = "";
                                String gpLocation = "";
                                String packingslipLabel = "";

                            %>

                            <nested:iterate property="salesOrderInvoiceLineItemList" id="invoiceLineItem" type="com.scalar.chiptrack.sales.SalesOrderInvoiceLineItem" >
                                <%

                                    //if( currentCustomerShippingID != invoiceLineItem.getCustomerShippingID() )
                                    if ( ! ( salesOrderNumber.equalsIgnoreCase(invoiceLineItem.getSalesOrderNumber()) && gpLocation.equalsIgnoreCase(invoiceLineItem.getGpLocation())) )
                                    {
                                        //currentCustomerShippingID = invoiceLineItem.getCustomerShippingID();
                                        salesOrderNumber = invoiceLineItem.getSalesOrderNumber();
                                        gpLocation = invoiceLineItem.getGpLocation();
                                %>
                                <TR BGCOLOR="#DEE1E2">
                                    <TD colspan="8" BGCOLOR="#DEE1E2" ALIGN="LEFT">

                                        <%--input type="checkBox" name="salesOrderSelect" value="< nested:write name="invoiceLineItem" property="customerShippingLabel"/>" onclick="javascript:packingSlipSelected( this )"/ --%>
                                        <%--input type="checkBox" name="salesOrderSelect" value="<nested:write name="invoiceLineItem" property="salesOrderNumber"/>"
                                        onclick="javascript:salesOrderSelected( this )"/ --%>

                                        <input type="radio" name="salesOrderSelect" value="<nested:write name="invoiceLineItem" property="salesOrderNumberAndGpLocation"/>"
                                        onclick="javascript:salesOrderSelected( this )"/>
                                        <!--nested:write name="invoiceLineItem" property="customerShippingLabel"/-->
                                        <nested:write name="invoiceLineItem" property="salesOrderNumber"/>
                                        &nbsp;&nbsp;&nbsp;
                                        Location : <nested:write name="invoiceLineItem" property="gpLocation"/>
                                        &nbsp;&nbsp;&nbsp;
                                        Customer Name : <nested:write name="invoiceLineItem" property="customerName"/>


                                    </TD>
                                </TR>
                                <TR>
                                    <TD colspan="9"></TD>
                                </TR>
                                <%
                                    }
                                    if( ! packingslipLabel.equalsIgnoreCase(invoiceLineItem.getCustomerShippingLabel() ) )
                                    {
                                        packingslipLabel =  invoiceLineItem.getCustomerShippingLabel();

                                 %>
                                <TR>
                                    <TD colspan="9">
                                        <input type="checkBox" name="packingSlipSelect" value="<nested:write name="invoiceLineItem" property="customerShippingLabel"/>" onclick="javascript:packingSlipSelected( this,'<nested:write name="invoiceLineItem" property="salesOrderNumberAndGpLocation"/>' )"/>
                                        <nested:write name="invoiceLineItem" property="customerShippingLabel" />
                                        <input type="hidden" name="salesOrder" value ="<nested:write name="invoiceLineItem" property="salesOrderNumberAndGpLocation"/>"/>
                                    </TD>
                                </TR>
                                <%
                                    }
                                %>
                                <TR width="100%">
                                    <TD width='8%' >&nbsp;</TD>
                                    <TD width='9%' align="right" >
                                        <!--nested:hidden property="customerShippingLabel"/-->
                                        <nested:hidden property="salesOrderNumberAndGpLocation"/>
                                        <nested:checkbox property="lineItemSave" styleClass="txfield" disabled="true" />&nbsp;
                                    </TD>
                                    <TD width='17%' align="center">
                                        <!--nested:text property="salesOrderNumber" styleClass="txfield" readonly="true" disabled="true" size="17"/-->
                                        <nested:text property="customerShippingLabel" styleClass="txfield" readonly="true" disabled="true" size="17"/>
                                    </TD>
                                    <TD width='20%' align="center">
                                        <nested:text property="marketingPartNo" styleClass="txfield" readonly="true" disabled="true"/>
                                    </TD>
                                    <TD width='25%' align="center">
                                        <nested:textarea property="lineItemDescription" styleClass="txfield" readonly="true" disabled="true"/>
                                    </TD>
                                    <TD width='8%' align="center">
                                        <nested:text property="quantity" styleClass="txfield" readonly="true" disabled="true" size="12"/>
                                    </TD>
                                    <TD width='8%' align="center">
                                        <nested:hidden property="unitPrice" ></nested:hidden>
                                        &nbsp;&nbsp;&nbsp;&nbsp;$<nested:text property="unitPriceAsString" styleClass="valuetxfield" readonly="true" disabled="true" size="12"/>
                                    </TD>
                                    <TD width='8%' align="center">
                                        <nested:hidden property="extendedPrice" ></nested:hidden>
                                        &nbsp;&nbsp;&nbsp;&nbsp;$<nested:text property="extendedPriceAsString" styleClass="valuetxfield" readonly="true" disabled="true" size="12"/>
                                    </TD>
                                    <TD width='8%' >&nbsp;</TD>
                                </TR>
                            </nested:iterate>

                            <TR>
                                <TD colspan="8"></TD>
                            </TR>
                        </TABLE>
                    </TD>
                </TR>
            </nested:nest>
            </html:form>
        </TABLE>
    </TR>
</TABLE>

</TD>
</TR>
</TABLE>
<!-- Your Work area ends here -->
<!-- Footer starts here -->
<%@include file="/jsp/includes/footer.jsp" %>
<!-- footer end here -->
</BODY>
</html>


